package com.koron.css2.projectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.impl.OpenUserAdd;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.projectCenter.bean.PjConsultBean;
import com.koron.css2.projectCenter.bean.PjMeterBean;
import com.koron.css2.projectCenter.dto.PjMeterSaveDto;
import com.koron.css2.projectCenter.mapper.PjConsultMapper;
import com.koron.css2.projectCenter.vo.PjMeterVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 用水咨询-多水表保存
 * @author 许海宜
 * 2020年3月27日
 */
public class PjMeterSave implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换为QueryBean
			PjMeterSaveDto dto = JsonUtils.objectToPojo(req.getData(), PjMeterSaveDto.class);
			//TODO 字段校验
			
			//检查用水咨询是否存在
			PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
			PjConsultBean pjConsultBean = pjConsultMapper.selectById(dto.getConsultId());
			if(pjConsultBean == null) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "用水咨询单据不存在", null);
			}
			CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
			//查询临时表
			CtmInfoBean ctmInfoBean = ctmMapper.getCtmInfoById(pjConsultBean.getUserCtmTempId(), 2);
			//删除关联关系
			pjConsultMapper.delMeter(dto.getConsultId());
			//获取Mapper实例
			UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
			String userId;//用户ID
			int installAmount = 0;
			for (PjMeterVO user : dto.getUserInfo()) {
				//判断用户主键是否为空
				if(StringUtils.isBlank(user.getId())) {
					//新增
					userId = Tools.getObjectId();
					user.setId(userId);
					user.setBusinessAbode(pjConsultBean.getBusinessAbode());
					user.setBusinessArea(pjConsultBean.getBusinessArea());
					user.setAdminArea(pjConsultBean.getAdminArea());
					user.setUseTimeLimit(pjConsultBean.getLimitDate());
					OpenUserAdd.ctmInfoToUserInfo(ctmInfoBean, user);

					user.setStatus("1");
					user.setCreateAccount(userInfo.getUserInfo().getAcount());
					user.setCreateName(userInfo.getUserInfo().getName());
					user.setOpenDate(Tools.getCurrentDate());
					user.setTenantId(userInfo.getCurWaterCode());
					user.setLadderPersonNum(4);
					user.setLastReadingNum(0);
					//生成用户编号
					user.setUserNo(Tools.getBillNo(factory, "USER_INFO", userInfo.getCurWaterCode()));
					//判断户表改造
					if(pjConsultBean.getPjType().equals("2")) {
						user.setReformFlag(1);
					}
					userInfoMapper.userInfoAdd(user, 2);
				} else {
					//修改
					userId = user.getId();
					user.setBusinessAbode(pjConsultBean.getBusinessAbode());
					user.setBusinessArea(pjConsultBean.getBusinessArea());
					user.setAdminArea(pjConsultBean.getAdminArea());
					user.setUseTimeLimit(pjConsultBean.getLimitDate());
					user.setUpdateAccount(userInfo.getUserInfo().getAcount());
					user.setUpdateTime(CommonUtils.getCurrentDate());
					user.setStatus("1");
					user.setLadderPersonNum(4);
					user.setLastReadingNum(0);
					OpenUserAdd.ctmInfoToUserInfo(ctmInfoBean, user);
					
					userInfoMapper.userInfoUpdate(user, 2);
				}
				//添加关联关系
				PjMeterBean pjMeterBean = new PjMeterBean();
				pjMeterBean.setId(Tools.getObjectId());
				pjMeterBean.setConsultId(dto.getConsultId());
				pjMeterBean.setUserInfoTempId(userId);
				pjMeterBean.setInflate(user.getInflate());
				pjMeterBean.setSecondaryWater(user.getSecondaryWater());
				pjConsultMapper.addMeter(pjMeterBean);
				installAmount++;
			}
			//更新单据表的装表数量
			pjConsultMapper.updateInstallAmount(pjConsultBean.getId(), installAmount);
			
			//输出结果
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
		} catch (Exception e) {
			factory.close(false);
			logger.error("用水咨询-多水表保存失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}


}
